﻿using HelloMinasan.Web.Song.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HelloMinasan.Business;
using HelloMinasan.Framework.Web;
using HelloMinasan.Domain;

namespace HelloMinasan.Web.Song.Controllers
{
    [Authorize]
    public class AdminUsersController : ApplicationController//BaseController
    {
        ////
        //private UserUnitWork _userunitWork = new UserUnitWork();
        private readonly string SearchModelCookieKey = "UserSearchModel";

        private IUserService userService;

        public AdminUsersController(IUserService userService)
        {
            this.userService = userService;
        }

        // GET: /AdminUsers/

        public ActionResult Index()
        {
            return View();
        }


        //[AllowAnonymous]
        public ActionResult List(UserSearchModel model)
        {     //FormCollection form       
            if (ModelState.IsValid)
            {
                if (model.UserName == null) { model.UserName = ""; }
                if (model.Email == null) { model.Email = ""; }
                if (model.DisplayName == null) { model.DisplayName = ""; }
                List<AdminUsersViewModel> lstView = new List<AdminUsersViewModel>();
                SetSearchCookie<UserSearchModel>(model, SearchModelCookieKey);
                lstView = Search(GetSearchModel<UserSearchModel>(SearchModelCookieKey));
                return View(lstView);

            }
            else
                return View("List", new List<AdminUsersViewModel>());
        }

        //[AllowAnonymous]
        public ActionResult GridviewPatial(UserSearchModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.UserName == null) { model.UserName = ""; }
                if (model.Email == null) { model.Email = ""; }
                if (model.DisplayName == null) { model.DisplayName = ""; }
                List<AdminUsersViewModel> lstView = new List<AdminUsersViewModel>();
                SetSearchCookie<UserSearchModel>(model, SearchModelCookieKey);
                lstView = Search(GetSearchModel<UserSearchModel>(SearchModelCookieKey));
                return PartialView(lstView);

            }
            else
                return PartialView(new List<AdminUsersViewModel>());
        }

        //[AllowAnonymous]
        public ActionResult Detail(int Userid)
        {
            //var query = _userunitWork.User.Find(Userid);
            var query = userService.Get(Userid);

            AdminUsersUpdate view = new AdminUsersUpdate();
            view.Userid = query.Id;
            view.UserName = query.Username;
            view.DisplayName = query.DisplayName;
            view.Email = query.Email;
            //view.Active = query.Active == "0" ? "UnActive" : "Active";
            view.Active = query.Status != UserStatus.Active ? "UnActive" : "Active";
            //ViewBag.Typeclocked = new SelectList(GetClock(), "ID", "Name", query.IsLocked);
            ViewBag.Typeclocked = new SelectList(GetClock(), "ID", "Name", query.Status == UserStatus.Blocked);
            return View(view);
        }

        private List<AdminUsersViewModel> Search(UserSearchModel model)
        {
            //var query = from Lea in _userunitWork.User.All.ToList()
            //            //.Where(n => CommonData.UnicodeConvert(n.Username.ToLower()).Contains(CommonData.UnicodeConvert(model.UserName.ToLower().Trim())))
            //            //&& CommonData.UnicodeConvert(n.Fullname.ToLower()).Contains(CommonData.UnicodeConvert(model.DisplayName.ToLower().Trim())) && CommonData.UnicodeConvert(n.Email.ToLower()).Contains(CommonData.UnicodeConvert(model.Email.ToLower().Trim())))
            //            select new AdminUsersViewModel
            //            {
            //                Userid = Lea.UserId,
            //                UserName = Lea.Username,
            //                Email = Lea.Email,
            //                DisplayName = Lea.Fullname,
            //                IsLocked = Lea.IsLocked == true ? "Clocked" : "Unclocked",
            //                Active = Lea.Active == "0" ? "Unactive" : "Active"
            //            };
            //return query.ToList();

            var users = userService.GetAll();
            List<AdminUsersViewModel> adminUsersViewModels = new List<AdminUsersViewModel>();

            foreach (User user in users)
            {
                AdminUsersViewModel adminUsersViewModel = new AdminUsersViewModel();
                adminUsersViewModel.Userid = user.Id;
                adminUsersViewModel.UserName = user.Username;
                adminUsersViewModel.Email = user.Email;
                adminUsersViewModel.DisplayName = user.DisplayName;
                adminUsersViewModel.IsLocked = user.Status == UserStatus.Blocked ? "Clocked" : "Unclocked";
                adminUsersViewModel.Active = user.Status != UserStatus.Active ? "Unactive" : "Active";

                adminUsersViewModels.Add(adminUsersViewModel);
            }

            return adminUsersViewModels;
        }

        public ActionResult Active(int Userid)
        {
            var query = userService.Get(Userid); //userunitWork.User.Find(Userid);
            query.Status = UserStatus.Active;// query.Active = UserActive.Active;
            userService.SaveOrUpdate(query);  //_userunitWork.User.Update(query);
            ViewBag.message = "Active successfully";
            return Redirect(@Url.Action("List", "AdminUsers"));
        }

        public ActionResult Clock(int Userid)
        {
            //var query = _userunitWork.User.Find(Userid);
            var query = userService.Get(Userid);
            //if (!query.IsLocked)
            //if (query.Status == UserStatus.Blocked)
            //{
            //    query.IsLocked = true;
            //}
            //else
            //{
            //    query.IsLocked = false;
            //}
            //_userunitWork.User.Update(query);

            ViewBag.message = "Active successfully";
            return Redirect(@Url.Action("List", "AdminUsers"));
        }

        public class Clocklist
        {
            public int ID { get; set; }
            public string Name { get; set; }
        }

        public static List<Clocklist> GetClock()
        {
            List<Clocklist> List = new List<Clocklist>(){
                        new Clocklist { ID = 0, Name = "Unclocked" }, 
                        new Clocklist { ID = 1, Name = "Clocked" }
                        };
            return List;
        }



    }
}
